package com.apes.fn.scm.merchant.settlement.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.merchant.settlement.service
 * @ClassName: FeeRate
 * @Author: pyb
 * @Description: 获取费率的服务
 * @CreateTime: 2021-07-23 11:48
 * @Version: 1.0
 */
@Service("feeRateService")
public class FeeRateService extends DomainService {

    /**
     * 获取供应链管理费
     *
     * @param companyId
     * @param classifyId
     * @return
     */
    public double getSupplyChainManagementFee(String companyId, String classifyId, String supplierId) {
        PartyRole company = SpringManager.getBean(PartyRoleRepository.class).findOne(companyId);
        Map map = MapUtil.mapper(
                "OPERATION", company.getParty().getCorporate().getOperation().getId(), "COMPANY_ID", companyId, "SUPPLIER_ID", supplierId, "CLASSIFY_ID", classifyId, "SYS_RQ", new Date(), "conditionStrategyId", "FW_GYL_SK"
        );
        Object obj = this.invoke("condition.execConditionStrategy", map);
        JSONObject result = new JSONObject(MapUtil.mapped(obj));
        return result.getDoubleValue("T001");
    }

    /**
     * 获取平台服务费
     *
     * @param merchantId
     * @return
     */
    public double getPlatformRules(String merchantId, Date date) {
        PartyRole company = SpringManager.getBean(PartyRoleRepository.class).findOne(merchantId);
        Map map = MapUtil.mapper(
                "OPERATION", company.getParty().getCorporate().getOperation().getId(), "COMPANY_ID", merchantId, "SYS_RQ", date, "conditionStrategyId", "FW_PT_SK"
        );
        Object obj = this.invoke("condition.execConditionStrategy", map);
        JSONObject result = new JSONObject(MapUtil.mapped(obj));
        if (!result.containsKey("T001")) throw new RuntimeException("获取平台服务费，没有获取到 T001 的值");
        return result.getDoubleValue("T001");
    }

    /**
     * 获取平台服务费(减免)
     *
     * @param merchantId
     * @return
     */
    public double getPlatformReducedRules(String merchantId,String classifyId,  Date date) {
        PartyRole company = SpringManager.getBean(PartyRoleRepository.class).findOne(merchantId);
        Map map = MapUtil.mapper(
                "OPERATION", company.getParty().getCorporate().getOperation().getId(), "COMPANY_ID", merchantId,"CLASSIFY_ID", classifyId, "SYS_RQ", date, "conditionStrategyId", "FW_JM_SK"
        );
        Object obj = this.invoke("condition.execConditionStrategy", map);
        JSONObject result = new JSONObject(MapUtil.mapped(obj));
//        if ("1".equals(result.getString("sys_break"))){
//            throw new RuntimeException("未成功获取价格！");
//        }
        return result.getDoubleValue("T001");
    }

    /**
     * 获取二手车平台服务费
     *
     * @param merchantId
     * @return
     */
    public double getUsedCarPlatformRules(String merchantId) {
        PartyRole company = SpringManager.getBean(PartyRoleRepository.class).findOne(merchantId);
        Map map = MapUtil.mapper(
                "OPERATION", company.getParty().getCorporate().getOperation().getId(), "COMPANY_ID", merchantId, "SYS_RQ", new Date(), "conditionStrategyId", "FW_PT_SK"
        );
        Object obj = this.invoke("condition.execConditionStrategy", map);
        JSONObject result = new JSONObject(MapUtil.mapped(obj));
        return result.getDoubleValue("T001");
    }

    /**
     * 获取天猫平台服务费
     *
     * @param merchantId
     * @return
     */
    public double getTmallPlatformRules(String merchantId) {
        PartyRole company = SpringManager.getBean(PartyRoleRepository.class).findOne(merchantId);
        Map map = MapUtil.mapper(
                "OPERATION", company.getParty().getCorporate().getOperation().getId(), "COMPANY_ID", merchantId, "SYS_RQ", new Date(), "conditionStrategyId", "FW_TM_SK"
        );
        Object obj = this.invoke("condition.execConditionStrategy", map);
        JSONObject result = new JSONObject(MapUtil.mapped(obj));
        return result.getDoubleValue("T001");
    }

    /**
     * 获取车行易费率
     *
     * @param merchantId
     * @return
     */
    public double getCXYRules(String merchantId) {
        PartyRole company = SpringManager.getBean(PartyRoleRepository.class).findOne(merchantId);
        Map map = MapUtil.mapper(
                "OPERATION", company.getParty().getCorporate().getOperation().getId(), "SYS_RQ", new Date(), "conditionStrategyId", "CXY_YJ"
        );
        Object obj = this.invoke("condition.execConditionStrategy", map);
        JSONObject result = new JSONObject(MapUtil.mapped(obj));
        return result.getDoubleValue("T001");
    }
}
